<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Think\Model;
use Org\Util;

class SortController extends AdminController
{

    public function sortindex()
    {
        //判断并更新计划进度
        $map['status'] = array('neq', -1);
        $res = M('sort_plan')->where($map)->select();
        for ($i = 0; $i < count($res); $i++) {
            $nowtime = NOW_TIME;
            $map['id'] = $res[$i]['id'];
            if ($nowtime < $res[$i]['planstart'] && $res[$i]['process'] != 4) {
                $data['process'] = 1;
                M('sort_plan')->where($map)->save($data);
            } elseif ($res[$i]['planstart'] <= $nowtime && $nowtime <= $res[$i]['planend'] && $res[$i]['process'] != 4) {
                $data['process'] = 2;
                M('sort_plan')->where($map)->save($data);
            } elseif ($nowtime >= $res[$i]['planend'] && $res[$i]['process'] != 4) {
                $data['process'] = 3;
                M('sort_plan')->where($map)->save($data);
            }
        }
        $this->meta_title = '分拣管理';
        $this->display();
    }

    public function staffindex()
    {
        $map['status'] = array('neq', -1);
        $res = M('sort_staff')->where($map)->select();
        $this->assign('_list', $res);
        $this->meta_title = '分拣员列表';
        $this->display();
    }

    public function staffadd()
    {
        if (IS_POST) {
            $data['staffname'] = I('staffname');
            $data['jobnumber'] = I('jobnumber');
            $model = M('sort_staff');
            $id = $model->add($data);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('staffindex'));
            }
        } else {
            $this->meta_title = "分拣员添加";
            $this->display();
        }
    }

    public function staffedit()
    {
        $map['id'] = I('id');
        if (IS_POST) {
            $data['staffname'] = I('staffname');
            $data['jobnumber'] = I('jobnumber');
            $model = M('sort_staff');
            $id = $model->where($map)->save($data);
            if (!$id) {
                $this->error("编辑失败！");
            } else {
                $this->success('编辑成功！', U('staffindex'));
            }
        } else {
            $res = M('sort_staff')->where($map)->find();
            $this->classify = $res;
            $this->meta_title = "分拣员编辑";
            $this->display();
        }
    }

    public function staffdel()
    {
        $map['id'] = I('id');
        $model = M('sort_staff');
        $data['status'] = -1;
        $id = $model->where($map)->save($data);
        if (!$id) {
            $this->error("删除失败！");
        } else {
            $this->success('删除成功！', U('staffindex'));
        }
    }

    public function planindex()
    {
        $map['status'] = array('neq', -1);
        //$res = M('sort_plan')->where($map)->order('id desc')->select();
        $res = $this->lists('sort_plan', $map, 'id desc');
        for ($i = 0; $i < count($res); $i++) {
            $nowtime = NOW_TIME;
            $map['id'] = $res[$i]['id'];
            if ($nowtime < $res[$i]['planstart'] && $res[$i]['process'] != 4) {
                $data['process'] = 1;
                M('sort_plan')->where($map)->save($data);
            } elseif ($res[$i]['planstart'] <= $nowtime && $nowtime <= $res[$i]['planend'] && $res[$i]['process'] != 4) {
                $data['process'] = 2;
                M('sort_plan')->where($map)->save($data);
            } elseif ($nowtime >= $res[$i]['planend'] && $res[$i]['process'] != 4) {
                $data['process'] = 3;
                M('sort_plan')->where($map)->save($data);
            }
        }
        $this->assign('_list', $res);
        $this->meta_title = '计划任务列表';
        $this->display();
    }

    public function planadd()
    {
        if (IS_POST) {
            $data['planname'] = I('planname');
            $data['remark'] = I('remark');
            $data['planstart'] = strtotime(I('planstart'));
            $data['planend'] = strtotime(I('planend'));
            $data['plancnt'] = 0;
            $data['realitycnt'] = 0;
            $model = M('sort_plan');
            $id = $model->add($data);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('planindex'));
            }
        } else {
            $this->meta_title = "添加分拣计划";
            $this->display();
        }
    }

    public function planedit()
    {
        $map['id'] = I('id');
        if (IS_POST) {
            $data['planname'] = I('planname');
            $data['remark'] = I('remark');
            $data['planstart'] = strtotime(I('planstart'));
            $data['planend'] = strtotime(I('planend'));
            $model = M('sort_plan');
            $id = $model->where($map)->save($data);
            if (!$id) {
                $this->error("编辑失败！");
            } else {
                $this->success('编辑成功！', U('planindex'));
            }
        } else {
            $res = M('sort_plan')->where($map)->find();
            $this->classify = $res;
            $this->meta_title = "分拣计划编辑";
            $this->display();
        }
    }

    public function plandel()
    {
        $map['id'] = I('id');
        $model = M('sort_plan');
        $data['status'] = -1;
        $id = $model->where($map)->save($data);
        if (!$id) {
            $this->error("删除失败！");
        } else {
            $this->success('删除成功！', U('planindex'));
        }
    }

    public function plandetail()
    {
        $map['id'] = I('id');
        $res = M('sort_plan')->where($map)->order('id desc')->find();
        $this->assign('data', $res);

        $mapb['planid'] = I('id');
        //$resa = M('sort_plandetail')->where($mapb)->order('id desc')->select();
        $resa = $this->lists('sort_plandetail', $mapb, 'id desc');
        $this->assign('_list', $resa);
        $this->display();
    }

    public function plandetailread()
    {
        $mapb['planid'] = I('id');
        $resa = $this->lists('sort_plandetail', $mapb, 'id desc');
        $this->assign('_list', $resa);
        $this->display();
    }

    public function missionadd()
    {
        $SERVERName = $_SERVER["SERVER_NAME"];
        if (IS_POST) {
            $mapd['BIN'] = I('BIN');
            $resd = M('material_product')->where($mapd)->find();
            $mape['planid'] = I('heheplanid');
            $mapf['id'] = I('heheplanid');
            $planId = I('heheplanid');
            $plan = M('sort_plan')->where($mapf)->find();
            $planEndTime = date('ymd', $plan['planend']);
            $sql = "select missionnumber from gr_sort_plandetail where missionnumber like '$planEndTime%' ORDER by id desc limit 1";
            $Model = new Model();
            $missionnumber = $Model->query($sql);
            if ($missionnumber != null && count($missionnumber) > 0) {
                $missionnumber = $missionnumber[0]['missionnumber'];
                $missionnumberSub = substr($missionnumber, 0, 6);
                $temp = substr($missionnumber, 6, 4);
                $temp = (floatval($temp) + 1) + 10000;
                $missionnumber = $missionnumberSub . substr($temp, 1, 4);
            } else {
                $missionnumber = $planEndTime . '0001';
            }
            $data['missionnumber'] = $missionnumber;
            //            $rescount = M('sort_plandetail')->where($mape)->select();
            //            $count = count($rescount) + 1;
            //            if ($count < 10) {
            //                $misnum = '00' . $count;
            //            } elseif ($count >= 10 && $count < 100) {
            //                $misnum = '0' . $count;
            //            } else {
            //                $misnum = $count;
            //            }
            //            $misdate = substr($rese['planname'], 2, 6);
            //            if (substr($rese['planname'], 8, 2) == 'am') {
            //                $data['missionnumber'] = $misdate . '1' . $misnum;
            //            } else {
            //                $data['missionnumber'] = $misdate . '2' . $misnum;
            //            }
            $data['planid'] = I('heheplanid');
            $data['plancnt'] = I('plancnt');
            $data['product'] = $resd['pname'];
            $data['spec'] = $resd['spec'];
            $data['productBIN'] = $resd['BIN'];
            $data['originalBIN'] = I('originalBIN');
            $data['originalbatch'] = I('warn_batch');

            $BIN = $data['originalBIN'];
            $originalbatch = $data['originalbatch'];
            $sql = "select * from gr_inventory_warn where BIN='$BIN' and warn_batch='$originalbatch' and delflag=0";
            $originalbatchCnt = $Model->query($sql);
            if ($originalbatchCnt != null && count($originalbatchCnt) > 0) {
                $data['createAt'] = NOW_TIME;
                $model = M('sort_plandetail');
                $id = $model->add($data);
                $mapcnt['id'] = I('heheplanid');
                $rescnt = M('sort_plan')->where($mapcnt)->find();
                $newcnt['plancnt'] = I('plancnt') + $rescnt['plancnt'];
                $idcnt = M('sort_plan')->where($mapcnt)->save($newcnt);
                if (!$id || !$idcnt) {
                    $this->error("添加失败！");
                } else {
                    if (strpos($SERVERName, "host") > 0) {
                        $this->success('添加成功！', '/erp/index.php?s=/admin/sort/plandetail/id/' . $planId);
                    } else {
                        $this->success('添加成功！', '/index.php?s=/admin/sort/plandetail/id/' . $planId);
                    }
                }
            }else{
                $this->error("所选原材料在所选批次中无库存，请重新选择！");
            }
        } else {
            $mapa['classify'] = '正价商品';
            $resa = M('material_product')->where($mapa)->select();
            $this->assign('list', $resa);
            $mapb['classify'] = '原包装';
            $resb = M('material_product')->where($mapb)->select();
            $this->assign('_list', $resb);
//            $mapc['status'] = '1';
//            $mapc['cnt'] = array('gt', 0);
//            $mapc['BIN'] = array('like', '1%');
//            $resc = M('inventory_warn')->where($mapc)->select();
//            $this->assign('_lista', $resc);
            $this->meta_title = "分拣任务添加";
            $heheplanid = I('planid');
            $this->assign('heheplanid', $heheplanid);
            $this->display();
        }
    }

    public  function getoriginbatch(){
        $originalBIN = I('originalBIN');
        $sql="select * from gr_inventory_warn WHERE BIN='$originalBIN' and status=1";
        $Model = new Model();
        $res = $Model->query($sql);
        $this->ajaxReturn($res);
    }

    public function  editmission($id = null)
    {
        $SERVERName = $_SERVER["SERVER_NAME"];
        if (IS_POST) {
            $plandetailId = I('plandetailId');
            $planId = I('planId');
            $plancnt = I('plancnt');
            $plancnt = floatval($plancnt);
            if ($plancnt <= 0) {
                $this->error("数量应为数字且大于0！");
                return;
            }
            $Model = new Model();
            $sql = "update gr_sort_plandetail set plancnt=$plancnt WHERE id=$plandetailId";
            $res = $Model->execute($sql);
            if ($res > 0) {
                //更新计划数量
                $sql="select planid from gr_sort_plandetail where id=$plandetailId";
                $planid= $Model->query($sql);
                $planid=$planid[0]["planid"];
                $sql="select sum(plancnt) as counts from gr_sort_plandetail where planid=$planid and status=1";
                $plancnt=$Model->query($sql);
                $plancnt=$plancnt[0]["counts"];
                $sql="update gr_sort_plan set plancnt=$plancnt where id=$planid";
                $res2 = $Model->execute($sql);
                if (strpos($SERVERName, "host") > 0) {
                    $this->success('添加成功！', '/erp/index.php?s=/admin/sort/plandetail/id/' . $planId);
                } else {
                    $this->success('添加成功！', '/index.php?s=/admin/sort/plandetail/id/' . $planId);
                }
            }
        } else {
            if ($id != null) {
                $Model = new Model();
                $sql = "select * from gr_sort_plandetail where id=$id";
                $list = $Model->query($sql);
                if ($list != null && count($list) > 0) {
                    $data = $list[0];
                    $this->assign('data', $data);
                }
            }
            $this->display();
        }
    }

    public function dailysurvey()
    {
        $today = NOW_TIME;
        $date = date('Ymd', $today);
        $dataBegin = strtotime($date . '00:00:00');
        $dataEnd = strtotime($date . '23:59:59');;
        $sql = "select * from gr_sort_plan where planend>$dataBegin and planend<$dataEnd and status=1";
        $res = $this->listsJ($sql);
//        $map['planname'] = array('like', $date . '%');
//        $res = M('sort_plan')->where($map)->select();
        $this->assign('_list', $res);
        $dailyplancnt = 0;
        $dailyrealitycnt = 0;
        for ($i = 0; $i < count($res); $i++) {
            $dailyplancnt += floatval($res[$i]['plancnt']);
            $dailyrealitycnt += floatval($res[$i]['realitycnt']);
        }
//        $dailyplancnt = $res[0]['plancnt'] + $res[1]['plancnt'];
//        $dailyrealitycnt = $res[0]['realitycnt'] + $res[1]['realitycnt'];
        $dailycomplete = $dailyrealitycnt / $dailyplancnt * 100;
        $this->assign('dailyplancnt', $dailyplancnt);
        $this->assign('dailyrealitycnt', $dailyrealitycnt);
        $this->assign('dailycomplete', $dailycomplete);
        $this->display();
    }

    public function dailydetail()
    {
        $map['planid'] = I('id');
        $res = M('sort_plandetail')->where($map)->select();
        $this->assign('_list', $res);
        $mapa['id'] = I('id');
        $data = M('sort_plan')->where($mapa)->find();
        $this->assign('data', $data);
        $this->display();
    }

    public function reportlist()
    {
        $map['report'] = array('neq', '');
        //$res = M('sort_plandetail')->where($map)->select();
        $res = $this->lists('sort_plandetail', $map, 'id desc');
        $this->assign('_list', $res);
        $this->display();
    }

    public function completeact()
    {
        $map['missionnumber'] = I('missionnumber');
        $list = $this->lists('sort_missiondetail', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $res = M('sort_plandetail')->where($map)->find();
        $this->assign('data', $res);
        $mape['id'] = $res['planid'];
        $date = M('sort_plan')->where($mape)->find();
        $this->assign('date', $date);
        $this->display();
    }

    public function rukuliushui()
    {
        $map['missionnumber'] = I('missionnumber');

        $list = $this->lists('sort_productact', $map);
        int_to_string($list);
        $this->assign('_list', $list);

        $res = M('sort_plandetail')->where($map)->find();
        $this->assign('data', $res);
        $this->display();
    }

    public function missionreport()
    {
        $mapa['id'] = I('id');
        if (IS_POST) {
            $map['id'] = I('id');
            $data['report'] = I('report');
            $model = M('sort_plandetail');
            $id = $model->where($map)->save($data);
            if (!$id) {
                $this->error("提交失败！");
            } else {
                $res = M('sort_plandetail')->where($map)->find();
                $mapc['id'] = $res['planid'];
                $date['process'] = 4;
                M('sort_plan')->where($mapc)->save($date);
                $this->success('提交成功！', U('planindex'));
            }
        } else {
            $resa = M('sort_plandetail')->where($mapa)->find();
            $this->assign('data', $resa);
            $this->assign('id', I('id'));
            $this->meta_title = "分拣员添加";
            $this->display();
        }
    }

    public function staffstatistics($startDate = null, $endDate = null)
    {
        if ($endDate == null) {
            $endDate = date("Y-m-d", time());
        }
        if ($startDate == null) {
            $startDate = $endDate;
        }
        $startDateStr = $startDate;
        $endDateStr = $endDate;
        $startTime = strtotime($startDate . " 00:00:00") * 1000;
        $endTime = strtotime($endDate . " 23:59:59") * 1000;
        $Model = new Model();
        $sql = "select * from (select s.jobnumber,s.staffname,count(f.id) as missionCount from gr_sort_productact f INNER  join gr_sort_staff s on f.staffname=s.staffname where f.createAt>$startTime and f.createAt<$endTime GROUP by f.staffname) t order by t.missionCount desc";
        $list = $Model->query($sql);
        $this->assign('_list', $list);
        $this->assign('startTime', $startDateStr);
        $this->assign('endTime', $endDateStr);
        $this->display();
    }

    public function staffdetail($staffname = null, $startDate = null, $endDate = null)
    {
        if ($staffname != null && $startDate != null && $endDate != null) {
            $startDateStr = $startDate;
            $endDateStr = $endDate;
            $startTime = strtotime($startDate . " 00:00:00");
            $endTime = strtotime($endDate . " 23:59:59");
            $Model = new Model();
            $sql = "select tt.missionnumber,tt.product,tt.spec,tt.staffname,sum(tt.cnt) as counts from (select t.missionnumber,t.product,t.spec,sm.staffname,sm.cnt from (SELECT * FROM `gr_sort_plandetail` where createAt>=$startTime and createAt<=$endTime and status=1) t inner join (select * from `gr_sort_missiondetail` where staffname='$staffname' and status>1) sm on t.missionnumber=sm.missionnumber)tt group by tt.staffname,tt.missionnumber";
            $list = $Model->query($sql);
            $this->assign('_list', $list);
            $this->assign('startTime', $startDateStr);
            $this->assign('endTime', $endDateStr);
            $this->assign('staffname', $staffname);
            $this->display();
        }
    }

    public function  sortmonth($startDate = null, $endDate = null)
    {
        if ($startDate != null && $endDate != null) {
            $startDateStr = $startDate;
            $endDateStr = $endDate;
            $startTime = strtotime($startDate . " 00:00:00");
            $endTime = strtotime($endDate . " 23:59:59");
            $Model = new Model();
            $sql = "select t.aday,sum(t.plancnt)as plancount,sum(t.realitycnt) as realcount,FORMAT(sum(t.realitycnt)/sum(t.plancnt)*100,2) as rate  from (select *,FROM_UNIXTIME(planend,'%Y-%m-%d') as aday from gr_sort_plan where planend>$startTime and planend<$endTime and status=1) t GROUP by t.aday";
            $list = $Model->query($sql);
            $this->assign('startTime', $startDateStr);
            $this->assign('endTime', $endDateStr);
            $this->assign('_list', $list);
        }
        $this->display();
    }

    public function  planbyday($aday = null)
    {
        if ($aday != null) {
            $startTime = strtotime($aday . " 00:00:00");
            $endTime = strtotime($aday . " 23:59:59");

            $Model = new Model();
            $sql = "select * from gr_sort_plan where planend>$startTime and planend<$endTime and status=1";
            $list = $Model->query($sql);
            $this->assign('_list', $list);
        }
        $this->display();
    }

    public function statisticenterstock($startDate = null, $endDate = null,$export=0)
    {
        if ($endDate == null) {
            $endDate = date("Y-m-d", time());
        }
        if ($startDate == null) {
            $startDate = $endDate;
        }
        $startDateStr = $startDate;
        $endDateStr = $endDate;
        $startTime = strtotime($startDate . " 00:00:00");
        $endTime = strtotime($endDate . " 23:59:59");
        $Model = new Model();
        $sql = "SELECT product,spec,productBIN,originalbatch,sum(realitycnt) as counts FROM `gr_sort_plandetail` where createAt>=$startTime and createAt<=$endTime and realitycnt>0 and status=1 group by productBIN,originalbatch";
        $list = $Model->query($sql);

        /*if($export==1){
            $res = M('kf_cuidan')->where($map)->order('createAt desc')->select();
        }else{
            $res = $this->lists('kf_cuidan',$map,'createAt desc');
        }*/

        if($export==1){
            $data=[];
            foreach ($list as $k=>$v){
                $dataArr=array(
                    'product'=>$v['product'],
                    'spec'=>$v['spec'],
                    'productBIN'=>$v['productBIN'],
                    'originalbatch'=>$v['originalbatch'],
                    'counts'=>$v['counts']
                );
                $data[]=$dataArr;
            }
            if ($data) {
                //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
                import("Org.Util.PHPExcel");
                import("Org.Util.PHPExcel.Writer.Excel5");
                import("Org.Util.PHPExcel.IOFactory.php");

                $filename = "入库统计";
                $headArr = array("成品名称", "规格", "成品编码", "原料批次", "入库数量");
                $title = "入库统计";
                $this->getExcel($filename, $headArr, $data, $title);
            }else{
                $this->error("没有可导出的数据！");
            }
        }else{
            $this->assign('_list', $list);
            $this->assign('startTime', $startDateStr);
            $this->assign('endTime', $endDateStr);
            $this->display();
        }
    }

    public function statisticoutstock($startDate = null, $endDate = null)
    {
        if ($endDate == null) {
            $endDate = date("Y-m-d", time());
        }
        if ($startDate == null) {
            $startDate = $endDate;
        }
        $startDateStr = $startDate;
        $endDateStr = $endDate;
        $startTime = strtotime($startDate . " 00:00:00");
        $endTime = strtotime($endDate . " 23:59:59");
        $Model = new Model();
        $sql = "select tt.product,tt.spec,tt.productBIN,tt.originalbatch,sum(tt.cnt) as counts from(select t.*,sm.cnt from (SELECT missionnumber,product,spec,productBIN,originalbatch FROM `gr_sort_plandetail` where createAt>=$startTime and createAt<=$endTime and status=1) t inner join (SELECT missionnumber,cnt FROM `gr_sort_missiondetail` where status=3) sm on t.missionnumber=sm.missionnumber) tt group by tt.productBIN,tt.originalbatch";
        $list = $Model->query($sql);
        $this->assign('_list', $list);
        $this->assign('startTime', $startDateStr);
        $this->assign('endTime', $endDateStr);
        $this->display();
    }

    public function exportstaffdetail($staffname = null, $startDate = null, $endDate = null)
    {
        if (!IS_POST) {
            $this->redirect("Sort/staffdetail");
        } else {
            if ($staffname == null||$startDate == null|| $endDate == null) {
                $this->error("参数有误！");
            } else {
                $dataTable = $this->getstaffdetail($staffname, $startDate, $endDate);
                if ($dataTable) {

                    //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
                    import("Org.Util.PHPExcel");
                    import("Org.Util.PHPExcel.Writer.Excel5");
                    import("Org.Util.PHPExcel.IOFactory.php");

                    $filename = "$staffname $startDate~$endDate 分拣明细";

                    $headArr = array("任务编号", "姓名", "商品名称", "规格", "分拣量");

                    $title = $filename;
                    $this->getExcel($filename, $headArr, $dataTable, $title);
                } else {
                    $this->error("没有可导出的数据！");
                }
            }
        }
    }

    private function getstaffdetail($staffname, $startDate, $endDate)
    {
        if ($staffname != null && $startDate != null && $endDate != null) {
            $startTime = strtotime($startDate . " 00:00:00");
            $endTime = strtotime($endDate . " 23:59:59");
            $Model = new Model();
            $sql = "select tt.missionnumber,tt.product,tt.spec,tt.staffname,sum(tt.cnt) as counts from (select t.missionnumber,t.product,t.spec,sm.staffname,sm.cnt from (SELECT * FROM `gr_sort_plandetail` where createAt>=$startTime and createAt<=$endTime and status=1) t inner join (select * from `gr_sort_missiondetail` where staffname='$staffname' and status>1) sm on t.missionnumber=sm.missionnumber)tt group by tt.staffname,tt.missionnumber";
            $list = $Model->query($sql);
            return $list;
        }
    }

    /**
     * 导出共用方法
     */
    public function getExcel($fileName, $headArr, $data, $title)
    {
        //对数据进行检验
        if (empty($data) || !is_array($data)) {
            //die("data must be a array");
        }
        //检查文件名
        if (empty($fileName)) {
            exit;
        }
        // H:i:s
        $date = date("Y_m_d_H_i_s", time());
        //$date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";

        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();

        //第一列设置报表时间范围
        $key = ord("A");
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $title);

        //第二列设置报表列头
        $key = ord("A");
        foreach ($headArr as $v) {
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '2', $v);
            $key += 1;
        }

        $column = 3;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach ($data as $key => $rows) { //行写入
            $span = ord("A");
            foreach ($rows as $keyName => $value) {// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j . $column, $value);
//                $objActSheet->getColumnDimension('a')->setWidth(20);
//                $objActSheet->getColumnDimension('b')->setWidth(50);
                $span++;
            }
            $column++;
        }

        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }
}

